#include "sockets_imp.hpp"

#include <string>
#include <sstream>

#ifdef SCK_WIN32

static int winsock_ref = 0;

void initSockets()
{
	if(++winsock_ref == 1)
	{
		WSADATA wsaData;
		int res;

		res = WSAStartup(MAKEWORD(2,2), &wsaData);
		if (res != 0)
		{
			return;
		}
	}
}

void shutdownSockets()
{
	if(--winsock_ref == 0)
	{
		WSACleanup();
	}
}

#endif

SocketException::SocketException(int error_) : error(error_)
{
	using namespace std;
	stringstream ss;
	switch (error)
	{
		case SCKER_ADDRESSINUSE:
			ss << "Socket Error [" << error << "]: Address already in use";
			whatStr = ss.str();
			break;
		default:
			ss << "Socket Error [" << error << "]";
			whatStr = ss.str();
	}
}

const char* SocketException::what() const throw()
{
	return whatStr.c_str();
}

int SocketException::getError() const
{
	return error;
}
